package vo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;

public class VendaDAO {
	public Integer salvarVenda(VendaVO venda) {

		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		Integer id = 0;
		if (venda.getId() != 0) {
			session.saveOrUpdate(venda);
		} else {
			id = (Integer) session.save(venda);
		}

		transaction.commit();
		return id;
	}

	@SuppressWarnings("unchecked")
	public List<VendaVO> consultarVenda(VendaVO venda) {
		List<VendaVO> listaDeVenda = new ArrayList<VendaVO>();

		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		Criteria criteria = session.createCriteria(VendaVO.class);

		 if (venda.getNome() != null &&
		 !venda.getNome().equals("")) {
		 criteria.add(Restrictions.ilike("nome", venda.getNome()+"%"));
		 }

		listaDeVenda = criteria.list();

		transaction.commit();
		return listaDeVenda;
	}

	public void excluir(VendaVO venda) {
		Transaction transaction = null;

		Session session = HibernateUtil.currentSession();
		transaction = session.beginTransaction();

		session.delete(venda);

		transaction.commit();
	}

}

// }
